package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.EmQueryParam;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@Slf4j//记录日志
@RequestMapping("/emps")
@RestController//@Controller+@ResponseBody
public class EmpController {

    @Autowired
    private EmpService empService;

    //分页查询/条件分页查询 实现
    @GetMapping//GetMapping 是 Spring MVC 注解，用于将 HTTP GET 请求映射到 list 方法，实现分页查询员工信息的功能。//返回数据给前端
    public Result list(EmQueryParam emQueryParam){
        log.info("查询：{}",emQueryParam);
        PageResult<Emp> pageResult = empService.page(emQueryParam);
        return Result.success(pageResult);
    }
    //新增员工实现
    @Log
    @PostMapping//PostMapping 是 Spring MVC 注解，用于将 HTTP POST 请求映射到 add 方法，实现员工信息的添加功能。//从前端接受数据
    public Result add(@RequestBody Emp emp){
        log.info("新增员工：{}",emp);
        empService.save(emp);
        return Result.success();
    }
    //删除员工实现
    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除员工：{}", ids);
        empService.delete(ids);
        return Result.success();
    }
    //根据id查询员工信息

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("查询员工：{}", id);
        Emp emp =  empService.getInfo(id);
        return Result.success(emp);
    }

    //修改员工
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工：{}", emp);
        empService.update(emp);
        return Result.success();
    }



}